<template>
	<view class="">
		<view class="content-wp">
			<textarea v-model='content' placeholder="请要反馈的内容(100字内)" :maxlength="100"
				style="box-sizing:border-box; hight:480rpx;font-size: 28rpx;width:100%; line-height: 45rpx;padding: 30rpx;" />
		</view>
		<view class="active-list">
			<view class="img-box">
				<u-upload accept="image" :fileList="fileListFeedback" @afterRead="afterRead" @delete="deletePic"
					name="Feedback" :multiple='true' :maxCount="9" width="200" height="200" imageMode="aspectFit">
					<view class="uploadWp">
						<u-icon name="plus" color="#c9cdd4" size="60"></u-icon>
						<text style="color: #c9cdd4;font-size: 24rpx;">点击上传</text>
					</view>
				</u-upload>
			</view>
			<view class="sign-up">
				<u-button type="primary" color="#276AEE" text="提交" :loading="submitLoad" throttleTime="600"
					@click="submitFeedback()">
				</u-button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import baseConfig from '@/config/baseUrl.js'
	export default {
		components: {

		},
		data() {
			return {
				fileListFeedback: [],
				pageNo: 1,
				pageSize: 10,
				submitLoad: false,
				content: ''
			}
		},
		onLoad(option) {

		},
		computed: {
			...mapGetters(['userInfo'])
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				console.log(url)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseConfig.uploadUrl + '/casugn-system/sys/common/upload',
						filePath: url,
						name: 'file',
						success: (res) => {
							let data = JSON.parse(res.data)
							if (data.success) {
								resolve(data.message)
							}
						}
					});
				})
			},
			// 提交
			async submitFeedback() {
				if (this.submitLoad === true) return
				this.submitLoad = true
				if (this.content == "") {
					uni.showToast({
						title: "请输入内容",
						icon: 'none'
					});
					return false
				}
				let imgArr = this.fileListFeedback.map(item => {
					return item.url
				})
				let image = ''
				if (imgArr.length > 0) {
					image = imgArr.join()
				}
				let payload = {
					name: this.userInfo.name,
					openId: this.userInfo.openId,
					content: this.content,
					image
				}
				let res = await this.$api.addFeedback(payload)
				if (res.success) {
					this.submitLoad = false
					uni.showToast({
						title: '提交成功',
					})
					uni.navigateBack()
				} else {
					this.submitLoad = false
					uni.showToast({
						title: res.message,
						icon: "none"
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-upload__success {
		display: none !important;
	}

	::v-deep .u-upload__deletable {
		right: 20upx;
		top: 20upx;
		width: 20upx;
		height: 20upx;
		text-align: center;
		border-bottom-left-radius: 0;

		.u-icon--right {
			text {
				font-size: 30upx !important;
				top: 8upx !important;
				left: 6upx !important;
			}
		}
	}

	.content-wp {
		margin: 30rpx;
		background-color: #f7f8fa;
	}

	.active-list {
		background: #FFFFFF;
		position: relative;

		.img-box {
			margin-top: 32rpx;
			margin-left: 32rpx;

			.uploadWp {
				width: 218rpx;
				height: 218rpx;
				background-color: #f7f8fa;
				justify-content: center;
				display: flex;
				flex-direction: column;
				align-items: center;
			}
		}


		.sign-up {
			width: 100%;
			padding: 16rpx 32rpx;
			padding-bottom: env(safe-area-inset-bottom);
			position: fixed;
			bottom: 0;
			height: 180rpx;
			background: #FFFFFF;
			z-index: 20;
			text-align: center;
			box-sizing: border-box;
			left: 0;

			::v-deep .u-button {
				margin: 0 auto;
				margin-top: 20rpx;
			}
		}
	}
</style>
